package com.wkbb.basic.controller.open;

import com.alibaba.fastjson.JSONObject;
import com.wkbb.basic.model.DeviceInfo;
import com.wkbb.basic.service.DeviceInfoService;
import com.wkbb.basic.service.UserFaceService;
import com.wkbb.basic.service.YouTuService;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.dto.basic.UserFaceDto;
import com.wkbb.common.utils.MapUtil;
//import com.wkbb.common.vo.DeviceUserFaceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luowenyan
 * @version V1.0
 * @className UserFaceController
 * @description 人脸管理
 * @date 2019/8/26 11:08
 */
@RestController
@Api(description = "人脸管理对外接口")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class UserFaceController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private YouTuService youTuService;

    @Autowired
    private UserFaceService userFaceService;

    @ApiOperation(value = "获取多个班级下学生人脸信息")
    @GetMapping(value = "getUserFaceList")
    public ResultDTO<List<UserFaceDto>> getUserFaceList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级列表", required = true) @RequestParam(name = "classIds", required = true) List<Long> classIds) {
        try{
            List<UserFaceDto> userFaceList = userFaceService.getUserFaceList(orgId,classIds);
            return new ResultDTO<List<UserFaceDto>>(userFaceList);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultDTO(false);
        }
    }


    @ApiOperation(value = "根据机构人员查询人脸信息")
    @GetMapping(value = "getUserByOrgPerson")
    public ResultDTO<UserDto> getUserByOrgPerson(
        @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
        @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
        @ApiParam(value = "人员ID", required = true) @RequestParam(name = "personId", required = true) String personId
    ){
        try{
            UserDto userDto = userFaceService.getUserByOrgPerson(MapUtil.newParamMap("orgId", orgId, "personId", personId));
            return new ResultDTO<>(userDto);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultDTO<>(false);
        }
    }

//    @ApiOperation(value = "获取学校的用户人脸列表")
//    @RequestMapping(value = "/getFaceListOfDevice", method = RequestMethod.GET)
//    public ResultDTO<List<DeviceUserFaceVo>> getFaceListOfDevice(
//            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
//            @ApiParam(value = "设备编号", required = true) @RequestParam(name = "deviceCode", required = true) String deviceCode) {
//        log.info("***********UserFaceController.getFaceList******apiVersion:{},deviceCode:{}", apiVersion, deviceCode);
//
//        try {
//            DeviceInfo deviceInfo = deviceInfoService.getInfoByDeviceCode(deviceCode);
//            if (deviceInfo == null) {
//                return new ResultDTO<>(ErrorCode.DEVICE_NOT_EXISTS);
//            }
//
//            List faceList = youTuService.getFaceListByOrgAndUser(deviceInfo.getOrgId(), null);
//            ResultDTO<List<DeviceUserFaceVo>>  resultDTO = new ResultDTO<>(faceList);
//            log.info("获取学校的用户人脸列表:{}", JSONObject.toJSONString(resultDTO));
//            return resultDTO;
//        } catch (Exception e) {
//            log.error("***********UserFaceController.getFaceList******", e);
//            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
//        }
//    }

    @ApiOperation(value = "人脸录入入口(去掉微众的逻辑，只注册到阅面算法)")
    @RequestMapping(value = "/addReadSenUserFace", method = RequestMethod.GET)
    public ResultDTO<Object> addReadSenUserFace(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "第一张正脸照片", required = true) @RequestParam(name = "picUrlA", required = true) String picUrlA,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "集团ID", required = true) @RequestParam(name = "accountId", required = true) Long accountId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value = "1,小程序,2后台管理", required = true) @RequestParam(name = "type", required = true) Integer type,
            @ApiParam(value = "人脸相似度(相似度为整数值:范围80-99)", required = false) @RequestParam(name = "similar", required = false) Short similar
    ) {
        try {
            if (null == picUrlA || null == userId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UserFaceController.addUserFaceForLocalRegist******apiVersion:{},picUrlA:{},orgId:{},accountId:{},userId:{},type:{},similar:{}", apiVersion, picUrlA,  orgId, accountId, userId, type, similar);

            Boolean result = userFaceService.addUserFace(picUrlA,  orgId, accountId, userId, type, similar);

            return new ResultDTO<>(result);
        } catch (Exception e) {
            log.error("***********UserFaceController.addUserFaceForLocalRegist******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸删除")
    @RequestMapping(value = "/deleteReadSenUserFace", method = RequestMethod.DELETE)
    public ResultDTO<Object> deleteReadSenUserFace(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "个体ID", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "orgId", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {
            if (null == personId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UserFaceController.deleteReadSenUserFace******apiVersion:{},personId:{}", apiVersion, personId);
            return new ResultDTO<>(userFaceService.deleteFace(personId,orgId));

        } catch (Exception e) {
            log.error("***********UserFaceController.deleteReadSenUserFace******", e);
            return new ResultDTO<>(false);
        }
    }
}
